
/**
 * 获取时间
 * @param {Object} obj
 */
export function getNowFormatAllDate(obj : any) {
	let date = new Date()
	if (obj !== null) {
		date = new Date(obj)
	}
	let seperator1 = '-'
	let seperator2 = ':'
	let month : number = date.getMonth() + 1
	let strDate : number = date.getDate()
	if (month >= 1 && month <= 9) {
		month = '0' + month
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = '0' + strDate
	}
	let hours = date.getHours()
	if (hours >= 0 && hours <= 9) {
		hours = '0' + hours
	}
	let minutes = date.getMinutes()
	if (minutes >= 0 && minutes <= 9) {
		minutes = '0' + minutes
	}
	let seconds = date.getSeconds()
	if (seconds >= 0 && seconds <= 9) {
		seconds = '0' + seconds
	}
	let timeStr = date.getFullYear() +
		seperator1 +
		month +
		seperator1 +
		strDate +
		' ' +
		hours +
		seperator2 +
		minutes +
		seperator2 +
		seconds
	return timeStr
}
/**
 * 获取时间
 * @param {Object} obj
 */
export function getNowFormatAllDate2(date : Date) {
	let seperator1 = '-'
	let seperator2 = ':'
	let month : number = date.getMonth() + 1
	let strDate : number = date.getDate()
	if (month >= 1 && month <= 9) {
		month = '0' + month
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = '0' + strDate
	}
	let hours = date.getHours()
	if (hours >= 0 && hours <= 9) {
		hours = '0' + hours
	}
	let minutes = date.getMinutes()
	if (minutes >= 0 && minutes <= 9) {
		minutes = '0' + minutes
	}
	let seconds = date.getSeconds()
	if (seconds >= 0 && seconds <= 9) {
		seconds = '0' + seconds
	}
	let timeStr = date.getFullYear() +
		seperator1 +
		month +
		seperator1 +
		strDate +
		' ' +
		hours +
		seperator2 +
		minutes +
		seperator2 +
		seconds
	return timeStr
}
/**
 * 获取时间
 * @param {Object} obj
 */
export function getNowDate(obj : string) {
	let date = new Date()
	if (obj.length > 0) {
		date = new Date(obj)
	}
	let seperator1 = '-'
	var month : number = date.getMonth() + 1
	var strDate : number = date.getDate()
	if (month >= 1 && month <= 9) {
		month = '0' + month
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = '0' + strDate
	}
	let timeStr = date.getFullYear() +
		seperator1 +
		month +
		seperator1 +
		strDate
	return timeStr
}
/**
 * 获取时间
 * @param {Object} obj
 */
export function getFristDate() {
	let date = new Date()
	let seperator1 = '-'
	var month : any = date.getMonth() + 1
	if (month >= 1 && month <= 9) {
		month = '0' + month
	}
	let timeStr = date.getFullYear() +
		seperator1 +
		month +
		seperator1 +
		'01'
	return timeStr
}
/**
 * 获取时间
 * @param {Object} obj
 */
export function getNowTime(obj : string) {
	let date = new Date()
	if (obj !== null) {
		date = new Date(obj)
	}
	let seperator2 = ':'
	let hours = date.getHours()
	if (hours >= 0 && hours <= 9) {
		hours = '0' + hours
	}
	let minutes = date.getMinutes()
	if (minutes >= 0 && minutes <= 9) {
		minutes = '0' + minutes
	}
	let seconds = date.getSeconds()
	if (seconds >= 0 && seconds <= 9) {
		seconds = '0' + seconds
	}
	let timeStr = hours +
		seperator2 +
		minutes +
		seperator2 +
		seconds
	return timeStr
}
/**
 * 获取时间
 * @param {Object} obj
 */
export function getNowTimeArr(obj : string) {
	let date = new Date()
	if (obj !== null && obj.length > 0) {
		date = new Date(obj)
	}
	let hours = date.getHours()
	if (hours >= 0 && hours <= 9) {
		hours = '0' + hours
	}
	let minutes = date.getMinutes()
	if (minutes >= 0 && minutes <= 9) {
		minutes = '0' + minutes
	}
	let seconds = date.getSeconds()
	if (seconds >= 0 && seconds <= 9) {
		seconds = '0' + seconds
	}
	return [hours, minutes, seconds]
}
/**
 * 获取时间
 * @param {Object} obj
 */
export function getNowTimeArr2(obj : any) {
	let date = new Date(obj)
	let hours = date.getHours()
	if (hours >= 0 && hours <= 9) {
		hours = '0' + hours
	}
	let minutes = date.getMinutes()
	if (minutes >= 0 && minutes <= 9) {
		minutes = '0' + minutes
	}
	let seconds = date.getSeconds()
	if (seconds >= 0 && seconds <= 9) {
		seconds = '0' + seconds
	}
	return [hours, minutes, seconds]
}

/**
 * 减法
 * @param {Object} arg1
 * @param {Object} arg2
 */
export function sub(arg1, arg2) {
	let r1, r2, m, c;
	try {
		r1 = arg1.toString().split(".")[1].length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch (e) {
		r2 = 0;
	}
	c = Math.abs(r1 - r2);
	m = Math.pow(10, Math.max(r1, r2));
	if (c > 0) {
		let cm = Math.pow(10, c);
		if (r1 > r2) {
			arg1 = Number(arg1.toString().replace(".", ""));
			arg2 = Number(arg2.toString().replace(".", "")) * cm;
		} else {
			arg1 = Number(arg1.toString().replace(".", "")) * cm;
			arg2 = Number(arg2.toString().replace(".", ""));
		}
	} else {
		arg1 = Number(arg1.toString().replace(".", ""));
		arg2 = Number(arg2.toString().replace(".", ""));
	}
	return (arg1 - arg2) / m;
}
/**
 * 加法
 * @param {Object} arg1
 * @param {Object} arg2
 */
export function sum(arg1, arg2) {
	let r1, r2, m, c;
	try {
		r1 = arg1.toString().split(".")[1].length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch (e) {
		r2 = 0;
	}
	c = Math.abs(r1 - r2);
	m = Math.pow(10, Math.max(r1, r2));
	if (c > 0) {
		let cm = Math.pow(10, c);
		if (r1 > r2) {
			arg1 = Number(arg1.toString().replace(".", ""));
			arg2 = Number(arg2.toString().replace(".", "")) * cm;
		} else {
			arg1 = Number(arg1.toString().replace(".", "")) * cm;
			arg2 = Number(arg2.toString().replace(".", ""));
		}
	} else {
		arg1 = Number(arg1.toString().replace(".", ""));
		arg2 = Number(arg2.toString().replace(".", ""));
	}
	return (arg1 + arg2) / m;
}
/**
 * 乘法
 * @param {Object} arg1
 * @param {Object} arg2
 */
export function multiply(arg1, arg2) {
	let m = 0,
		s1 = arg1.toString(),
		s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length;
	} catch (e) { }
	try {
		m += s2.split(".")[1].length;
	} catch (e) { }
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
/**
 * 除法
 * @param {Object} arg1
 * @param {Object} arg2
 */
export function division(arg1, arg2) {
	let m = 0,
		s1 = arg1.toString(),
		s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length;
	} catch (e) { }
	try {
		m += s2.split(".")[1].length;
	} catch (e) { }
	return Number(s1.replace(".", "")) / Number(s2.replace(".", "")) / Math.pow(10, m);
}
export function getAfterDate(n : number) {
	var n = n;
	var d = new Date();
	var year = d.getFullYear();
	var mon = d.getMonth() + 1;
	var day = d.getDate();
	if (day <= n) {
		if (mon > 1) {
			mon = mon - 1;
		} else {
			year = year - 1;
			mon = 12;
		}
	}
	d.setDate(d.getDate() + n);
	year = d.getFullYear();
	mon = d.getMonth() + 1;
	day = d.getDate();
	let s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
	return s;
}
export function getBeforeDate(n : number) {
	var n = n;
	var d = new Date();
	var year = d.getFullYear();
	var mon = d.getMonth() + 1;
	var day = d.getDate();
	if (day <= n) {
		if (mon > 1) {
			mon = mon - 1;
		} else {
			year = year - 1;
			mon = 12;
		}
	}
	d.setDate(d.getDate() - n);
	year = d.getFullYear();
	mon = d.getMonth() + 1;
	day = d.getDate();
	let s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
	return s;
}
export function getLocationCarIcon(pos : any) {
	if (pos.offlineState) {
		//离线
		return "https://www.qfclwt.com/public/map/icon_offline.png"
	} else {
		if (pos.spd == 0) {
			//在线
			return "https://www.qfclwt.com/public/map/icon_online.png"
		} else {
			//在线
			return "https://www.qfclwt.com/public/map/icon_top.png"
		}
	}
}
export function bd09Togc02(bdLat, bdLon) {
	let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
	var x = bdLon - 0.0065, y = bdLat - 0.006;
	var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
	var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
	var gcjLon = z * Math.cos(theta);
	var gcjLat = z * Math.sin(theta);
	return [gcjLat, gcjLon];
}
/**
* 设置坐标点位
*/
export function getPositionMarker(id : number, latitude : any, longitude : any, name : string, icon : string) {
	let lat = parseFloat(latitude)
	if (lat >= 90) {
		lat = 30
	}
	let lon = parseFloat(longitude)
	if (lon >= 180) {
		lon = 103
	}
	// let pos = bd09Togc02(lat, lon)
	//console.log(pos)
	return {
		id: id,
		latitude: lat,
		longitude: lon,
		width: 30,
		height: 35,
		label: {
			content: name,
			color: '#FFFFFF',
			anchorY: -60,
			anchorX: -30,
			bgColor: '#00000099',
			borderRadius: 30,
			padding: 5
		},
		iconPath: icon
	}
}
/**
* 设置坐标点位
*/
export function getTrucksMarker(obj : any) {
	let rotate = obj.drc
	let isRun = true
	let iconPath = 'https://www.qfclwt.com/public/map/icon_top.png'
	if (obj.offlineState) {
		//离线
		rotate = 0
		isRun = false
		iconPath = "https://www.qfclwt.com/public/map/icon_offline_new.png"
	} else {
		if (obj.spd == 0) {
			//停车
			rotate = 0
			isRun = false
			iconPath = "https://www.qfclwt.com/public/map/icon_online_new.png"
		} else {
			//在线
			isRun = true
			iconPath = "https://www.qfclwt.com/public/map/icon_top.png"
		}
	}
	return {
		id: obj.id,
		latitude: obj.lat,
		longitude: obj.lon,
		width: isRun ? 30 : 15,
		height: isRun ? 25 : 30,
		rotate: rotate,
		label: {
			content: obj.plateNum ? obj.plateNum : obj.vno,
			color: '#000000',
			anchorY: isRun ? -70 : -80,
			anchorX: -30,
			bgColor: '#FFCC00EE',
			borderRadius: 10,
			padding: 10,
			fontSize: 12,
			borderColor: '#999999',
			borderWidth: 1
		},
		iconPath: iconPath
	}
}
export function calcDistance(lat1 : number, lon1 : number, lat2 : number, lon2 : number) {
	const R = 6371e3; // 地球半径
	const φ1 = (lat1 * Math.PI) / 180; // 第一个点的纬度转化为弧度
	const φ2 = (lat2 * Math.PI) / 180; // 第二个点的纬度转化为弧度
	const Δφ = ((lat2 - lat1) * Math.PI) / 180; // 两点纬度之差转化为弧度
	const Δλ = ((lon2 - lon1) * Math.PI) / 180; // 两点经度之差转化为弧度

	const a =
		Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
		Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
	const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
	const distance = R * c; // 距离，单位为米
	return distance
}